
#ifndef __FLASH_H__
#define __FLASH_H__

#include <stdint.h>
#include "os.h"
#include "spi.h"

#define FLASH_TYPE_LIST(M) \
    M(FLASH_TYPE_ONCHIP) \
    M(FLASH_TYPE_W25QXX)

#define FLASH_TYPE_ENUM(e) e,

typedef enum {
    FLASH_TYPE_LIST(FLASH_TYPE_ENUM)
    FLASH_TYPE_MAX
} flash_type_e;

typedef struct {
    spi_port_e spi_port;
    gpio_pin_e cs_pin;
} flash_cfg_t;

typedef struct {
    uint32_t start_addr;
    uint32_t size;
    uint32_t block_size;
} flash_info_t;

typedef struct
{
    OS_MUTEX mutex;
    flash_cfg_t cfg;
    flash_info_t info;
    int(*open)(flash_cfg_t *config);
	int(*write)(uint32_t address, uint8_t *data, uint32_t len);
	int(*read)(uint32_t address, uint8_t *data, uint32_t len);
    int(*erase)(uint32_t address, uint32_t len);
    void(*close)(void);
    void(*deinit)(void);
} flash_t;

extern flash_t *flash_init(flash_type_e flash_type);
extern flash_t *flash_find_by_type(flash_type_e type);

#endif
